<?php

/**
 * Implements fiasco database template for
 * postgres connectivity.
 */

require_once 'drivers/psql.recordset.php';

class db extends fiasco_database_template
{

	/**
 	 * Info about driver type
	 */ 
	public $about = "Postgres Driver for Fiasco Framework - 2008";
	
	/**
	 * postgreSQL connection resource
	 */
	static protected $con = null;
	
	/**
	 * attemps to connect a database
	 * from the credentials provided
	 * by fiasco_config
	 */
	static protected function connect(){
		if(self::$con != null) return self::$con;
		$info = parse_url(DB_URL);
		$connection_string = "host=".$info->host." user=".$info->user." dbname=".substr($info->path, 1);
		if (!empty($info->pass)) $connection_string .= "pass=".$info->pass;
		if (self::$con = pg_connect($connection_string)){
			return self::$con;
		}
		throw new Exception("Fiasco PGSQL driver failed to make a connection to database");
	}
	
	/**
	 */
	public function query(){
		$args = func_get_args();
		$SQL = array_shift($args);
		$SQL = $this->prepare($SQL, $args);		
		return new psql_recordset(pg_query(self::$con, $SQL));
	}
	
	/**
	 * kills database connection
	 */
	static public function kill(){
		return pg_close(self::$con);
	}
}
